package cn.zh.stan.activity;

import android.app.Dialog;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import cn.zh.stan.BaseUrl;
import cn.zh.stan.R;
import cn.zh.stan.util.Loading;
import cn.zh.stan.util.RegexUtils;
import cn.zh.stan.util.ToastUtil;
import okhttp3.Request;
import okhttp3.Response;

public class ForgetPass extends BaseActivity {

    ImageView psback;
    EditText rephone;
    Button next;
    ImageView rpimg;
    LinearLayout rplin;

    private Dialog LDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pass);
        initv();
        }

    public void initv(){
        psback= (ImageView) findViewById(R.id.psback);
        psback.setOnClickListener(this);
        rephone= (EditText) findViewById(R.id.rephone);
        next= (Button) findViewById(R.id.next);
        rpimg= (ImageView) findViewById(R.id.rpimg);
        rplin= (LinearLayout) findViewById(R.id.rplin);
        next.setOnClickListener(this);
        rpimg.setOnClickListener(this);
        rephone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
               }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
               }
            @Override
            public void afterTextChanged(Editable editable) {
                int len=editable.length();
                if (len>0){
                    rpimg.setVisibility(View.VISIBLE);
                    rplin.setBackgroundResource(R.drawable.cprimbg);
                }else {
                    rpimg.setVisibility(View.INVISIBLE);
                    rplin.setBackgroundResource(R.drawable.graybg);
                  }
               }
           });
       }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.psback:
                finish();
                break;
            case R.id.next:
               LDialog= Loading.createLoadingDialog(this,"加载中...");
               getPost();
                break;
            case R.id.rpimg:
                rephone.getText().clear();
                rplin.setBackgroundResource(R.drawable.graybg);
                rpimg.setVisibility(View.INVISIBLE);
                break;
            }
          }


    public void getPost() {
        final String moblie = rephone.getText().toString();
        boolean ismobile = RegexUtils.checkMobile(moblie);
        if (ismobile){
            OkHttpUtils.post(BaseUrl.pass_find)
                    .params("moblie", moblie)
                    .execute(new StringCallback() {
                        @Override
                        public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                            System.out.println("------------->忘记密码发送验证码==" + s);
                            try {
                                Loading.closeDialog(LDialog);
                                JSONObject js = new JSONObject(s);
                                boolean status = js.getBoolean("State");
                                if (status) {
                                    boolean Flag = js.getBoolean("Flag");
                                    boolean Mark = js.getBoolean("Mark");
                                    if (Flag) {
                                        Intent intent = new Intent(ForgetPass.this, FinPasst.class);
                                        intent.putExtra("name", moblie);
                                        startActivity(intent);
                                       }
                                    String Msg = js.getString("Msg");
                                    ToastUtil.showShort(ForgetPass.this, Msg);
                                  } else {
                                    String msg = js.getString("msg");
                                    ToastUtil.showLong(ForgetPass.this, msg);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
              }else {
            ToastUtil.showLong(ForgetPass.this,"手机号错误");
           }
       }




}
